#pragma once
#include<string>
#include<queue>
#include"Connection.h"
#include<mutex>
#include<thread>
#include<functional>
#include<atomic>
#include<condition_variable>

class ConnectionPool{
public:
    static ConnectionPool* getConnectionPool();
    std::shared_ptr<Connection> getConnection();
    ~ConnectionPool();
private:
    void operator=(const ConnectionPool&) = delete;
    ConnectionPool(const ConnectionPool&) = delete;
    ConnectionPool();
    //生产新链接
    void produceConnectionTask();
    //扫描空闲连接
    void scanConnectionTask();
    //加载配置
    bool loadConfigFile();

private:
    std::string _ip;
    unsigned short _port;
    std::string _username;
    std::string _password;
    std::string _dbname;
    int _intiConnectSize;   //初始连接数量
    int _maxConnectSize;    //最大连接数量
    int _maxIdletiom;       //最大空闲时间
    int _connectionTimeout; //超时时间

    std::queue<Connection*> _connectQue;    //存储mysql连接队列
    std::mutex _queueMutex; 
    std::condition_variable _cv;
    std::atomic_int _connectCnt;    //记录连接所创建的connection总量

    bool isRun = false;     //是否在运行

};
